/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "PLJavaDescriptorParser.h"

#include <filetypes/PLJavaConstTypesUtf8.h>
#include <filetypes/PLJavaDescriptorValueVoid.h>
#include <filetypes/PLJavaDescriptorValueByte.h>
#include <filetypes/PLJavaDescriptorValueChar.h>
#include <filetypes/PLJavaDescriptorValueDouble.h>
#include <filetypes/PLJavaDescriptorValueFloat.h>
#include <filetypes/PLJavaDescriptorValueInt.h>
#include <filetypes/PLJavaDescriptorValueLong.h>
#include <filetypes/PLJavaDescriptorValueShort.h>
#include <filetypes/PLJavaDescriptorValueBoolean.h>
#include <filetypes/PLJavaDescriptorValueArray.h>
#include <filetypes/PLJavaDescriptorValueReference.h>

using namespace PLSDK;

PLJavaDescriptorParser::PLJavaDescriptorParser(const PLJavaConstTypesUtf8 *descriptor) : m_descriptor(descriptor), m_parameter(), m_return(0) {
    const char *string = m_descriptor->string();
    int strLen = strlen(string);
    bool startParameter = false;
    for(int i = 0 ; i < strLen ; i++) {
        if(string[i] == '(') {
            startParameter = true;
        } else if(string[i] == ')') {
            startParameter = false;
        } else if(startParameter) {
            PLJavaDescriptorValue *v = parseValue(string, &i);
            if(v) {
                m_parameter.add(v);
            } else {
                printf("Java descriptor value not found.");
            }
        } else {
            m_return = parseValue(string, &i);
        }
    }
}

PLJavaDescriptorParser::~PLJavaDescriptorParser() {
    if(m_return) {
        delete m_return;
    }
    for(pluint i = 0 ; i < m_parameter.count() ; i++) {
        delete m_parameter[i];
    }
}

const PLJavaDescriptorValue *PLJavaDescriptorParser::parameter(pluint index) {
    PLJavaDescriptorValue *ret = m_parameter.get(index);
    if(!ret) {
        printf("The descriptor type at index %d was not found.", index);
        return 0;
    }
    return ret;
}

PLJavaDescriptorValue *PLJavaDescriptorParser::parseValue(const char *string, int *i) {
    const char str = string[*i];
    switch(str) {
        case 'B':
            return new PLJavaDescriptorValueByte();
        case 'C':
            return new PLJavaDescriptorValueChar();
        case 'D':
            return new PLJavaDescriptorValueDouble();
        case 'F':
            return new PLJavaDescriptorValueFloat();
        case 'I':
            return new PLJavaDescriptorValueInt();
        case 'J':
            return new PLJavaDescriptorValueLong();
        case 'L':
        {
            ++(*i);
            const char *start = string + *i;
            int len = 0;
            while(string[*i] != ';') {
                ++(*i);
                len++;
                if(string[*i] == '\0') {
                    printf("Error in descriptor parser, class name incorrect %s.", string);
                    return 0;
                }
            }
            char *ret = new char[len + 1];
            memcpy(ret, start, len);
            ret[len] = '\0';
            return new PLJavaDescriptorValueReference(ret);
        }
        case 'S':
            return new PLJavaDescriptorValueShort();
        case 'Z':
            return new PLJavaDescriptorValueBoolean();
        case '[':
            ++(*i);
            return new PLJavaDescriptorValueArray(parseValue(string, i));
        case 'V':
            return new PLJavaDescriptorValueVoid();
        default:
            return 0;
    }
    return 0;
}
